<?php 

//require_once '../include/config.lib.php';
//require_once '../include/database.lib.php';

	ConnectToDB();

if(isset($_POST['checkpost'])){
//	register_reviewers(); ::
	
	$i = 1;
	while (isset($_POST["remail_".$i]) && $_POST["remail_".$i] != "") {
		
		$password = strtoupper(SHA1($_POST["rpass_".$i]));
		$insert = DBExecute("INSERT INTO DB2INST1.person (USERNAME, NAME, EMAIL, PASSWORD) 
							   VALUES('".$_POST["rusrname_".$i]."', '".$_POST["rname_".$i]."', '".$_POST["remail_".$i]."', '".$password."')");
					
		$selectid = DBExecute("SELECT id FROM person WHERE username = '".$_POST["rusrname_".$i]."'");
		$id = $selectid->fetchAssocRow();
		
		DBExecute("INSERT INTO DB2INST1.peopleroles (personid,role) 
							   VALUES(".$id["ID"].", 'reviewer')");
				
		$i++;
	}
	$_POST['checkpost'] = null;
	
	
	
	$selectid = DBExecute("SELECT id FROM person WHERE username = '".$_SESSION["accountNo"]."'");
	$id = $selectid->fetchAssocRow();//find the person's id to redirect to the person's own page
	$url = "./person_view.php?id=".$id["ID"]."#maintabs-6";
	echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$url."'\"></HTML>";
}


/// show existing reviewers:
if($_SESSION['role'] != 'pcchair')
	return;
	

//$password = strtoupper(SHA1($_POST["password"]));//encrypt password by SHA1 and upper case.
$reviewersReq = DBExecute("SELECT username, name, email, ID FROM person, peopleroles WHERE role = 'reviewer' AND id = personid");


$rev_rows = "";
while($reviewers = $reviewersReq->fetchAssocRow()){
	$rev_rows .= "<tr><td>".$reviewers['USERNAME']."</td><td>".$reviewers['NAME']."</td><td>".$reviewers['EMAIL']."</td></tr>";
}

//echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/conference_admin.css\" />";

?>
  <dev id="existing_reviewers">
		<p>Existing reviewers:</br>
		<table id="revs" > 
		<tr>
			<th>username</th><th>name</th><th>email</th> 
		</tr><?php echo $rev_rows?></table></p>
	 </dev>
<?php 
	
//show ends
	
//register_new_reviewers() :

	?>
	<a href="#" onclick="javascript:showElement('addReviewers')">Register Reviewers</a>				
					
	<div id="addReviewers" style="display:none;" class = "section">
		<form id="register_reviewers" action="" method="post">					
			<table id="newr">
				<tr><th>username</th><th>name</th><th>email</th><th>password</th></tr>
	<?php 
	$j = 1;
	for ($i = 0; $i < 5; $i++, $j++) {		
		
		$revidRec = DBExecute("SELECT id FROM person WHERE username = 'Reviewer".$j."'");
		while( $revidRec->fetchAssocRow() ){
			$j++;
			$revidRec = DBExecute("SELECT id FROM person WHERE username = 'Reviewer".$j."'");
		}
	?>
		<tr>
			<td><input value="Reviewer<?php echo $j?>" type="text" name="rusrname_<?php echo ($i+1)?>" size="15" readonly/></td>
			<td><input type="text" name="rname_<?php echo ($i+1)?>" size="15"/></td>
			<td><input type="text" name="remail_<?php echo ($i+1)?>" size="30"/></td>
			<td><input type="text" name="rpass_<?php echo ($i+1)?>" size="10"/></td>
		</tr>
	<?php 
	}
	?>
		</table>
			<input type="hidden" name="checkpost" value=true />
				<input type="submit" class="button" value="Add as reviewers" />
	</form>	</div>
	
	</br>
	<?php 


?>


